package com.crishne.anwo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crishne.anwo.constant.JsonStringConstant;
import com.crishne.anwo.model.Menu;
import com.crishne.anwo.model.Role;
import com.crishne.anwo.model.RoleMap;
import com.crishne.anwo.model.UserMap;
import com.crishne.anwo.service.MenuService;
import com.crishne.anwo.service.RoleMapService;
import com.crishne.anwo.service.RoleService;
import com.crishne.anwo.utill.CommonUtill;
import com.crishne.anwo.utill.PaginationUtil;

@Controller
@RequestMapping("sysmgr/role/")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private RoleMapService roleMapService;
	
	private CommonUtill commonUtill = new CommonUtill();


	//返回jsp视图
	@RequestMapping(value = "role_main")
	public String role_main(){
		return "admin/sysmgr/role_main";
	}
	
	@RequestMapping("getRoleEdit")
	public void getRoleEdit(HttpServletResponse response,HttpServletRequest request)
	{
		String roleId = request.getParameter("roleid");
		Role tRole = roleService.selectRoleById(roleId);
		
		HashMap retData = new HashMap();
		retData.put("roleinfo", tRole);
		retData.put("allmenus", menuService.getAllMenus());
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(retData);
		commonUtill.OutputJson(jsonString,response,request);
	}
    
    @RequestMapping("saveOrEditRole")
	public void saveOrEditRole(HttpServletResponse response,HttpServletRequest request){
		//获取参数
    	String roleId = request.getParameter("roleId");
		String roleName = request.getParameter("roleName");
		String nmOrder = request.getParameter("nmOrder");
		
		//名称不允许重复
		Role checkRole = roleService.selectRoleByName(roleName);
		if(null == roleId || "".equals(roleId)){
			if(null != checkRole){
				JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("该角色名已经存在，保存失败！");

				commonUtill.OutputJson(jsonString, response, request);
		        return;
			}
		}
		else{
			if(null != checkRole && !checkRole.getStRoleId().equals(roleId)){
				JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("该角色名已经存在，保存失败！");

				commonUtill.OutputJson(jsonString, response, request);
		        return;
			}
		}
		
		Role tRole = null;
		if(null == roleId || "".equals(roleId)){
			tRole = new Role();
		}
		else{
			tRole = roleService.selectRoleById(roleId);
		}
		tRole.setStRoleName(roleName);
		
		if(null != nmOrder && !"".equals(nmOrder)){
			tRole.setNmOrder(Integer.valueOf(nmOrder));
		}
		else{
			tRole.setNmOrder(0);
		}
		
		if(null == roleId || "".equals(roleId)){
			tRole.setStRoleId(UUID.randomUUID().toString());
			roleService.addRole(tRole);
		}
		else{
			roleService.updateRole(tRole);
		}
		
		JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("保存成功！");

		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("deleteRole")
    public void deleteRole(HttpServletResponse response,HttpServletRequest request){
    	String stRoleIds = request.getParameter("roleId");
    	JSONObject jsonString = new JSONObject();
    	try{
    		roleService.deleteRole(stRoleIds);
    		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除成功！");
    	}
    	catch(Exception ex){
    		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除失败！");
    	}
    	
		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("getRolePageList")
	public void getRolePageList(HttpServletResponse response,HttpServletRequest request){
		//获取当前页 和 一页显示的数据数目
		String conCurrentPage = request.getParameter("currentpage");
		String stPageSize = request.getParameter("pageSize");
		String stRoleName = request.getParameter("rolename");
		Integer currentpage = 1;
		Integer pageSize = 10;
		if(conCurrentPage != null && !"".equals(conCurrentPage)){
			currentpage = Integer.valueOf(conCurrentPage);
		}
		if(stPageSize != null && !"".equals(stPageSize)){
			pageSize = Integer.valueOf(stPageSize);
		}
		
		@SuppressWarnings("rawtypes")
		HashMap searchCondition = new HashMap();
		searchCondition.put("condDefault","1=1");
		if(null !=stRoleName && !"".equals(stRoleName) ){
			searchCondition.put("condRoleName","st_role_name like '%"+stRoleName+"%'");
		}
		//构造 PaginationUtil
		PaginationUtil<Role> pageUtil = new PaginationUtil<Role>();
		pageUtil.setCurrentPage(currentpage);
		pageUtil.setPageSize(pageSize);
		pageUtil.setStOrderBy(" nm_order desc");
		pageUtil.setSearchCondition(searchCondition);
		PaginationUtil<Role> rolePageList = roleService.searchRoles(pageUtil);
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(rolePageList);

		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("getAllRoles")
	public void getAllRoles(HttpServletResponse response,HttpServletRequest request)
	{
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(roleService.getAllRoles());
		commonUtill.OutputJson(jsonString,response,request);
	}
	
	@RequestMapping("getRoleMapMenu")
    public void getRoleMapMenu(HttpServletResponse response,HttpServletRequest request){
    	String roleId = request.getParameter("roleid");
    	//User tUser = userService.getUserById(userId);
    	List<RoleMap> roleMapList = roleMapService.getRoleMapMenus(roleId);
    	List<Menu> menuList = menuService.getAllMenus();
    	HashMap retMap = new HashMap();
    	retMap.put("roleMap", roleMapList);
    	retMap.put("allmenus", commonUtill.ChageMenuListToJson(menuList));
    	JSONObject jsonString = JsonStringConstant.getSuccessMessage(retMap);
		commonUtill.OutputJson(jsonString, response, request);
    }
    
    @RequestMapping("saveRoleMapMenus")
	public void saveRoleMapMenus(HttpServletResponse response,HttpServletRequest request) throws Exception{
		//获取参数
    	String roleId = request.getParameter("roleId");
		String roleMapMenus = request.getParameter("menumaps");
		String[] menuIds = roleMapMenus.split(",");
		roleMapService.saveMenuMaps(roleId,menuIds);
		
		
		JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("保存成功！");

		commonUtill.OutputJson(jsonString, response, request);
    }

}